package Aufgabe1_gleichwertige_loesungen;

import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FileOperations {
    static Random RANDOMIZER = new Random();
    static String DIRECTORY = "./folge.dat";
    
    public static void createZufallsFolge(int lowerBound, int upperBound, int anzahl) throws FileNotFoundException, IOException{
	    File datei = new File(DIRECTORY);
	    Writer fw = new FileWriter(datei);
        Writer bw = new BufferedWriter(fw);

	    for (int i=0;i<anzahl;i++) {
		    int zufallszahl = (lowerBound + RANDOMIZER.nextInt((upperBound+1) - lowerBound));
		    bw.write(zufallszahl + " ");
	}
	bw.flush();
    }
    public static List<Integer> readZufallsFolge()throws FileNotFoundException, IOException{
        List<Integer> result = new LinkedList();
        FileReader fr = new FileReader(DIRECTORY);
        BufferedReader br = new BufferedReader(fr);
        Scanner scanner = new Scanner(br);
        
        while(scanner.hasNext()){
            result.add(scanner.nextInt());
        }
        return new ArrayList(result);
    }
    
    public static void writeMessdaten(ArrayList<Object> messdaten){
        FileWriter fw = null;
        try {
            File datei = new File("./maxteilsummeTest_temp.csv");
            boolean exists=false;
            if(datei.exists()){exists=true;}
            fw = new FileWriter(datei, true);
            BufferedWriter bw = new BufferedWriter(fw);
            if(!exists){
                bw.write("Anzahl;Algorithmus;maxTeilsumme;Index1;Index2;Zeit (inkl. Zählen);Zeit (exkl. Zählen);Summe aller Zugriffe");
            }
            bw.newLine();
            for(int i=0;i<messdaten.size();i++ ){
                bw.append(messdaten.get(i).toString()+";");
            }
            bw.flush();
        } catch (IOException ex) {
            Logger.getLogger(FileOperations.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fw.close();
            } catch (IOException ex) {
                Logger.getLogger(FileOperations.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


        // diest ist ein 2. test
    }
    
    public static void updateMessdaten(ArrayList<String> updateStrings) throws FileNotFoundException, IOException{
        FileReader fr = new FileReader("./maxteilsummeTest_temp.csv");
        BufferedReader br = new BufferedReader(fr);
        
        File datei = new File("./maxteilsummeTest.csv");
        FileWriter fw = new FileWriter(datei, true);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.append(br.readLine());
        bw.newLine();
        
        for(String to_update : updateStrings){
            
            bw.append(br.readLine().replace("N/A", to_update));
            bw.newLine();
        }
        bw.flush();
        fw.close();
    }
}
